<template>
  <j-modal
    :title="title"
    :width="width"
    :visible="visible"
    :confirmLoading="confirmLoading"
    switchFullscreen
    @ok="handleCancel"
    @cancel="handleCancel"
    cancelText="关闭"
  >
    <a-card :bordered="false">
      <!-- 操作按钮区域 -->
      <div class="table-operator">
        <a-button type="primary" icon="download" @click="handleDelete(mainId)" v-show="dataSource.length > 0"
          >删除
        </a-button>
        <a-button type="primary" icon="download" @click="handleExportXls('补差记录')">导出</a-button>
      </div>

      <!-- table区域-begin -->
      <div>
        <a-table
          ref="table"
          size="middle"
          :scroll="{ x: true }"
          bordered
          rowKey="id"
          :columns="columns"
          :dataSource="dataSource"
          :pagination="ipagination"
          :loading="loading"
          :rowSelection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange }"
          class="j-table-force-nowrap"
          @change="handleTableChange"
        >
        </a-table>
      </div>
    </a-card>
  </j-modal>
</template>

<script>
import '@/assets/less/TableExpand.less'
import { mixinDevice } from '@/utils/mixin'
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import { deleteAction, getAction } from '@api/manage'

export default {
  name: 'HfPurchaseCompensateRecordList',
  mixins: [JeecgListMixin, mixinDevice],
  props: {
    mainId: {
      type: String,
      default: ''
    }
  },
  watch: {
    mainId: {
      handler(val) {
        if (val) {
          this.loadData(val)
        }
      },
      immediate: true
    }
  },
  data() {
    return {
      description: '补差记录管理页面',
      title: '补差记录',
      width: 1200,
      visible: false,
      confirmLoading: false,

      // 表头
      columns: [
        {
          title: '#',
          dataIndex: '',
          key: 'rowIndex',
          width: 60,
          align: 'center',
          customRender: function(t, r, index) {
            return parseInt(index) + 1
          }
        },
        {
          title: '单号',
          align: 'center',
          dataIndex: 'serial'
        },
        {
          title: '物料代码',
          align: 'center',
          dataIndex: 'materialCode'
        },
        {
          title: '物料名称',
          align: 'center',
          dataIndex: 'materialName'
        },
        {
          title: '规格型号',
          align: 'center',
          dataIndex: 'sap'
        },
        {
          title: '单位',
          align: 'center',
          dataIndex: 'unit'
        },
        {
          title: '原价',
          align: 'center',
          dataIndex: 'price'
        },
        {
          title: '补差价',
          align: 'center',
          dataIndex: 'compensatePrice'
        },
        {
          title: '差价',
          align: 'center',
          dataIndex: 'spread'
        },
        {
          title: '数量',
          align: 'center',
          dataIndex: 'number'
        },
        {
          title: '差价总额',
          align: 'center',
          dataIndex: 'totalSpread'
        },
        {
          title: '源差价方案id',
          align: 'center',
          dataIndex: 'sourceId'
        },
        {
          title: '操作',
          dataIndex: 'action',
          align: 'center',
          fixed: 'right',
          width: 147,
          scopedSlots: { customRender: 'action' }
        }
      ],
      url: {
        list: '/purchase/hfPurchaseCompensatePrice/getCompensateRecord',
        delete: '/purchase/hfPurchaseCompensatePrice/deleteCompensateRecord',
        exportXlsUrl: '/sale/hfCompensateRecord/exportXls'
      },
      dictOptions: {},
      superFieldList: []
    }
  },
  created() {},
  computed: {},
  methods: {
    initDictConfig() {},
    handleCancel() {
      this.$emit('close')
      this.visible = false
    },
    loadData(id) {
      console.log('id', id)
      if (id === undefined || id === null) {
        return
      }
      if (!this.url.list) {
        this.$message.error('请设置url.list属性!')
        return
      }
      var params = this.getQueryParams() //查询条件
      params.id = id
      this.loading = true
      getAction(this.url.list, params)
        .then(res => {
          if (res.success) {
            //update-begin---author:zhangyafei    Date:20201118  for：适配不分页的数据列表------------
            this.dataSource = res.result.records || res.result
            if (res.result.total) {
              this.ipagination.total = res.result.total
            } else {
              this.ipagination.total = 0
            }
            //update-end---author:zhangyafei    Date:20201118  for：适配不分页的数据列表------------
          } else {
            this.$message.warning(res.message)
          }
        })
        .finally(() => {
          this.loading = false
        })
    },
    handleDelete: function(id) {
      if (!this.url.delete) {
        this.$message.error('请设置url.delete属性!')
        return
      }
      var that = this
      deleteAction(that.url.delete, { id: id })
        .then(res => {
          if (res.success) {
            //重新计算分页问题
            that.reCalculatePage(1)
            that.$message.success(res.message)
            that.loadData(id)
            that.onClearSelected()
          } else {
            that.$message.warning(res.message)
          }
        })
        .finally(() => {
          that.handleCancel()
        })
    }
  }
}
</script>
<style scoped>
@import '~@assets/less/common.less';
</style>
